﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Web;
using System.Configuration;
namespace GaiNo.Core.Helpers
{
    public static class AuthenticationHelper
    {
        /// <summary>
        /// 保存登录票据
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="userData">The user data.>>>>>>GroupId|UserId|UserName|UserEmail|UserSalt|UserRole</param>
        public static void SetTicket(string name, string userData)
        {
            string domain = FormsAuthentication.CookieDomain;//ConfigHelper.GetConfig("Domain");
            SetTicket(name, userData, domain);
        }
        /// <summary>
        /// Sets the ticket.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="userData">The user data.</param>
        /// <param name="domain">The domain.</param>
        public static void SetTicket(string name, string userData, string domain)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, name, DateTime.Now, DateTime.Now.AddMinutes(30), true, userData);
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
            cookie.Expires = ticket.Expiration;
            cookie.Domain = domain;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 获取或设置用户登录票据
        /// </summary>
        public static string Ticket
        {
            get
            {
                if (IsAuthenticated)
                {
                    return ((FormsIdentity)HttpContext.Current.User.Identity).Ticket.UserData;
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                string strTicket = value;
                SetTicket(value, string.Empty);
            }
        }
        public static bool IsInRole(string roleName)
        {
            if (HttpContext.Current.User == null || !HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return false;
            }
            return HttpContext.Current.User.IsInRole(roleName);
        }
        /// <summary>
        /// 获取用户是否登录
        /// </summary>
        public static bool IsAuthenticated
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    return true;
                }
                return false;
            }
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        public static void Logout()
        {
            FormsAuthentication.SignOut();
            string name = FormsAuthentication.FormsCookieName;
            if (CookieHelper.HasCookie(name))
                CookieHelper.DeleteCookie(name, Domain);
            HttpContext.Current.Session.Clear();
        }
        public static string Domain
        {
            get
            {
                string url = HttpContext.Current.Request.Url.Host;
                string[] strs = url.Split('.');
                string domain = string.Empty;

                if (strs.Length == 3)
                    domain = strs[1] + "." + strs[2];
                else if (strs.Length == 2)
                    domain = url;
                return domain;
            }
        }
    }
}
